package zhtt.redis.session2.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import zhtt.redis.session2.config.RedisUtil;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@RestController
public class RedisController {

    @Autowired
    private RedisUtil redisUtil;
 
    @RequestMapping("/set")
    public void set(@RequestParam(value="k", defaultValue = "k") String key,
                    @RequestParam(value = "v", defaultValue = "v") String value){
        redisUtil.setValue(key, value);
    }
 
    @RequestMapping("/get")
    public Object get(@RequestParam(value="k", defaultValue = "k") String key){
        return redisUtil.getValue(key);
    }

    @RequestMapping(value = "/query", method = RequestMethod.GET)
    public Object sessions (HttpServletRequest request){
        Map<String, Object> map = redisUtil.getAllConfig();
        map.put("sessionId", request.getSession().getId());
        return map;
    }
}
